package com.example.yy.zyfypt_223yy.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ActionMenuItemView;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import com.example.yy.zyfypt_223yy.R;
import com.example.yy.zyfypt_223yy.bean.SessionID;
import com.example.yy.zyfypt_223yy.iface.CollectListener;
import com.example.yy.zyfypt_223yy.model.CollectModel;


public class ViewCaseActivity extends AppCompatActivity {
    private int resid;
    private String userid;
    private Boolean flagcollect=false;//收藏标志
    private Boolean flagfocus=false;
    private CollectModel collectmodel;//收藏model
    private WebView webView;
    Context context;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_case);
        System.out.println("----查看案例详情");

        resid  = getIntent().getIntExtra("resid",1);
        userid = getIntent().getStringExtra("userid");

        webView = (WebView)findViewById(R.id.webview);
        webView.loadUrl(BASEURL+"tcase.php/show/index/id/"+resid);
    }
    CollectListener listener=new CollectListener() {
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            ActionMenuItemView item=(ActionMenuItemView)findViewById(R.id.menucollect);
            //根据mode中response返回的字符串区分返回结果
            switch (msg)
            {
                case "2": System.out.println("----收藏成功");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "1":
                    System.out.println("----收藏失败");
                    break;
                case "5":
                    System.out.println("----取消收藏成功");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                case "4":
                    System.out.println("----取消收藏失败");
                    break;
                case "7":
                    System.out.println("----已收藏");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "8":
                    System.out.println("----未收藏");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);//加载菜单布局
        collectmodel=new CollectModel();//实例化对象
        collectmodel.exist("article",resid, SessionID.SessionID,listener);//判断是否收藏

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.menucollect:
                //Toast.makeText(this, "文章收藏", Toast.LENGTH_SHORT).show();
                if(flagcollect)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollect("article",resid, SessionID.SessionID,listener);
                }
                else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备收藏");
                    collectmodel.collect("article",resid, SessionID.SessionID,listener);
                }
                break;
            case R.id.menufocus:
                //Toast.makeText(this, "文章关注", Toast.LENGTH_SHORT).show();

                if(flagfocus)//如果已关注，则调用取消关注
                {
                    System.out.println("----准备取消关注");

                }
                else//如果未关注，则调用关注
                {
                    System.out.println("----准备关注");
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}